package com.triptogether.utils.facebook;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.facebook.model.GraphUser;

public class FacebookSharedPreferences {

	private final static String FACEBOOK_SHARED_PREFERENCES = "facebook_shared_preferences";
	private final static String FACEBOOK_PROPERTY_KEY_USER_FACEBOOK_ID = "property_facebook_id";
	private final static String FACEBOOK_PROPERTY_KEY_USER_NAME = "property_user_name";

	static SharedPreferences getFacebookSharedPreferences(Context context) {
		return context.getSharedPreferences(FACEBOOK_SHARED_PREFERENCES,
		                                    Context.MODE_PRIVATE);
	}

	/**
	 * Save the needed data of the current user that connected to facebook in the application.</br>
	 * The data is saved in {@link SharedPreferences} and can be get by the methods in {@link FacebookSharedPreferences}
	 * @param context
	 * @param user the current user (that connected to the application with facebook) to save it's data.
	 */
	public static void saveUserData(Context context, GraphUser user) {
		// Get the user name and the facebook id
		String userName = user.getName();
		String facebookId = user.getId();

		// Save the user data on facebook shared preferences
		Editor editor = getFacebookSharedPreferences(context).edit();
		editor.putString(FACEBOOK_PROPERTY_KEY_USER_FACEBOOK_ID, facebookId);
		editor.putString(FACEBOOK_PROPERTY_KEY_USER_NAME, userName);
		editor.commit();
	}

	/**
	 * Get the userName from the shared preferences.
	 * always return String, cannot return null.
	 * @param context
	 * @return The userName of the current user
	 */
	public static String getUserName(Context context) {
		String userName = getFacebookSharedPreferences(context).getString(FACEBOOK_PROPERTY_KEY_USER_NAME,
		                                                                                            "");
		userName = userName == null ? "" : userName;
		return userName;
	}

	/**
	 * Get the user's facebook Id.
	 * @param context
	 * @return
	 */
	public static String getUserFacebookId(Context context) {
		String userFacebookId = getFacebookSharedPreferences(context).getString(FACEBOOK_PROPERTY_KEY_USER_FACEBOOK_ID,
		                                                                        "");

		return userFacebookId;
	}

}
